package TUILayer;
import ControlLayer.*;
import ModelLayer.*;
import java.util.*;


/**
 * Write a description of class SaleUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SaleUI
{
    // instance variables - replace the example below with your own
    private static SaleCtrl saleCtrl = new SaleCtrl();
    private static MainMenuUI mmu = new MainMenuUI();

    /**
     * Constructor for objects of class SaleUI
     */
    public SaleUI()
    {
        // initialise instance variables
    }

    public void startMainMenu()
    {
        boolean _exit = false;
        while (!_exit) //! means while exit not is true (that is: false)
        {
            int _choice = writeMainMenu();
            if(_choice == 1)
            {
                createSale();
            }
            else
            {
                if(_choice == 2)
                {
                    getSale();
                }
                else
                {
                    if(_choice == 3)
                    {
                        updateSale();
                    }
                    else
                    {
                        if(_choice == 4)
                        {
                            removeSale();
                        }
                        else 
                        {
                            if(_choice == 5)
                            {
                                listAllSales();
                            }
                            else
                            {
                                if(_choice == 6)
                                {
                                    getPayment();
                                }
                                else
                                {
                                    if(_choice == 7)
                                    {
                                        mmu.mainMenuStart();
                                        _exit = true;
                                    }
                                    else
                                    {
                                        
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
           
    private int writeMainMenu()
    {       
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** SALE MENU ***");
            System.out.println(" (1) Create Sale");
            System.out.println(" (2) Show Sale Information");
            System.out.println(" (3) Update Sale");
            System.out.println(" (4) Delete Sale");
            System.out.println(" (5) List All Sales");
            System.out.println(" (6) Get Sale Payment");
            System.out.println(" (7) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            
            return choice;
    }
    
    private long inputSaleId()
    {   
        boolean ok = false;
        long id = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Sale ID:  ");
           try
           {
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
           }
        }//end while
        
        return id;
    }
    
    private long inputCustomerId()
    {   
        boolean ok = false;
        long customerId = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Customer ID:  ");
           try
           {
             customerId = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
             //keyboard.nextLine();
           }
        }//end while
        
        return customerId;
    }
    
    private long inputSellItemId()
    {   
        boolean ok = false;
        long id = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write Sell Item ID:  ");
           try
           {
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
           }
        }//end while
        
        return id;
    }
    
    private int inputQuantity()
    {   
        boolean ok = false;
        int quantity = 0;
        while (!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write quantity:  ");
           try
           {
               quantity = keyboard.nextInt();
               if(quantity > 0)
               {
                   ok = true;
               }
               else
               {
                   System.out.println("Quantity number must be bigger than 0. Please try again");
               }
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number. Please try again");
           }
        }//end while
        
        return quantity;
    }
    
    private String inputDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type date: ");
        String date = keyboard.nextLine();
        
        return date;
    }
    
    private String inputDescription()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type description: ");
        String description = keyboard.nextLine();
        
        return description;
    }
    
    public void getPayment()
    {
        long saleId = inputSaleId();
        if(saleCtrl.getSale(saleId) != null)
        {
            System.out.println("Total payment is: " + saleCtrl.getSale(saleId).getPayment());
        }
        else
        {
            System.out.println("Sale with this ID does not exist!");
        }
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    public void createSalesList(long _id)
    {
        Scanner keyboard = new Scanner(System.in);
        long sellItemId = inputSellItemId();
        if(saleCtrl.getSellItem(sellItemId) != null)
        {
            int quantity = inputQuantity();
            while(quantity > saleCtrl.getSellItem(sellItemId).getStock())
            {  
                System.out.println("There are not enough items in the stock! Availiable - " + saleCtrl.getSellItem(sellItemId).getStock());
                quantity = keyboard.nextInt();
            }

            SalesList salesList = new SalesList();
            salesList.setSellItem(saleCtrl.getSellItem(sellItemId));
            salesList.setQuantity(quantity);
            int stock = salesList.getSellItem().getStock() - quantity;
            salesList.getSellItem().setStock(stock);
            saleCtrl.getSale(_id).createSalesList(salesList);  
            
            System.out.println("SalesList has been created!");
        }
        else
        {
            System.out.println("Sell Item with this ID does not exist!");
        }
    }
    
    private void getSale()
    {
        System.out.println(" --- Show Sale information ---");
        long id = inputSaleId();
        if(saleCtrl.getSale(id) != null)
        {
            Sale s = saleCtrl.getSale(id);
            System.out.println("ID: " + s.getId());
            System.out.println("Date: " + s.getDate());
            System.out.println("Description: " + s.getDescription());
            System.out.println("Sale list(s): ");
            for (int i=0; i<s.listAllSalesLists().size(); i++)
            {  
                System.out.print ("     " + i + 1 +")" + "  Sell Item: " + s.listAllSalesLists().get(i).getSellItem().getName());
                System.out.print("  Quantity: " + s.listAllSalesLists().get(i).getQuantity());
                System.out.println("  Price per item: " + s.listAllSalesLists().get(i).getSellItem().getPrice());
            }
        }
        else
        {
            System.out.println("Sale with this ID does not exist!");
        }
        
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void createSale()
    {
        System.out.println(" --- Create Sale ---");
        long saleId = inputSaleId();
        if(saleCtrl.getSale(saleId) == null)
        {
            long customerId = inputCustomerId();
            if(saleCtrl.getCustomer(customerId) != null)
            {
                Sale s = new Sale();
                s.setId(saleId);
                s.setDate(inputDate());
                s.setDescription(inputDescription());
                s.setCustomer(saleCtrl.getCustomer(customerId));
                saleCtrl.createSale(s);
                while(saleCtrl.getSale(saleId).listAllSalesLists().size() == 0)
                {
                    createSalesList(saleId);
                }
                int nrPurchases = saleCtrl.getCustomer(customerId).getNrPurchases();
                saleCtrl.getCustomer(customerId).setNrPurchases(nrPurchases + 1);
                System.out.println("Sale has been created!");
            }
            else
            {
                System.out.println("Customer with this ID does not exist");
            }
        }
        
        else
        {
            System.out.println("Sale with this ID already exists");
        }
        
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void updateSale()
    {
        boolean exit = false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choice = updateSaleMenu();
            if(choice == 1)
            {
                updateSaleInformation();
            }
            else
            {
                if(choice == 2)
                {
                    long saleId = inputSaleId();
                    if(saleCtrl.getSale(saleId) != null)
                    {
                        createSalesList(saleId);
                    }
                    else
                    {
                        System.out.println("Sale with this ID does not exist!");
                        Scanner keyboard = new Scanner(System.in);
                        keyboard.nextLine();
                    }
                }
                else
                {
                    if(choice == 3)
                    {
                        System.out.println("Sale update is finished!");
                        exit = true;
                    }
                }
            }
        }
        
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private int updateSaleMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Sale update menu ***");
        System.out.println(" (1) Update sale information");
        System.out.println(" (2) Add sale list to sale");
        System.out.println(" (3) Finish update");
        int choice = keyboard.nextInt();
            
        return choice;
    }
    
    private void updateSaleInformation()
    {
        System.out.println(" --- Update Sale Information ---");
        long id = inputSaleId();
        if(saleCtrl.getSale(id) != null)
        {
            Sale s = new Sale();
            s.setId(id);
            s.setDate(inputDate());
            s.setDescription(inputDescription());
            
            saleCtrl.updateSale(s);
            System.out.println("Sale has been updated!");
        }
        
        else
        {
            System.out.println("Sale with this ID does not exist!");
        }
        
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void removeSale()
    {
        System.out.println(" --- Remove Sale ---");
        long id = inputSaleId();
        if(saleCtrl.getSale(id) != null)
        {
            for (int i=0; i<saleCtrl.getSale(id).listAllSalesLists().size(); i++)
            {  
                int stock = saleCtrl.getSale(id).listAllSalesLists().get(i).getSellItem().getStock();
                int quantity = saleCtrl.getSale(id).listAllSalesLists().get(i).getQuantity();
                saleCtrl.getSale(id).listAllSalesLists().get(i).getSellItem().setStock(stock + quantity); 
            }
            long customerId = saleCtrl.getSale(id).getCustomer().getId();
            int nrPurchases = saleCtrl.getCustomer(customerId).getNrPurchases();
            saleCtrl.getCustomer(customerId).setNrPurchases(nrPurchases - 1);
            saleCtrl.removeSale(id);
            System.out.println("Sale has been removed!");
        }
        else
        {
            System.out.println("Sale with this ID does not exist!");
        }
        
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    
    private void listAllSales()
    {
        System.out.println(" --- List all Sales ---");
        if (saleCtrl.listAllSales().size() == 0 )
        {
            System.out.println("There are no Sales in the system");
        }
        else
        {
            System.out.println("Results:");
            for (int i=0; i<saleCtrl.listAllSales().size(); i++)
            {  
                System.out.println (i+1 +")" + "  ID: " + saleCtrl.listAllSales().get(i).getId() + "  Description: " + saleCtrl.listAllSales().get(i).getDescription());
            }
        }
        
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
}
